{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "1787d11c-bd92-4e4e-9312-977bb7bd7e4e",
   "metadata": {},
   "source": [
    "# Walk-through: `prepare_state`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f6d752fa-ba4b-4d63-9ed8-fec358db31b7",
   "metadata": {},
   "source": [
    "This notebook is the Classiq SDK equivalent of the walk-through sequence as presented in the Classiq web IDE [[1](#Classiq_IDE)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "361ae02e-c0ef-4d0f-bfab-bef5557fab59",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T14:27:38.031376Z",
     "iopub.status.busy": "2024-05-07T14:27:38.028772Z",
     "iopub.status.idle": "2024-05-07T14:27:40.841618Z",
     "shell.execute_reply": "2024-05-07T14:27:40.840035Z"
    }
   },
   "outputs": [],
   "source": [
    "from classiq import (\n",
    "    Output,\n",
    "    QArray,\n",
    "    QBit,\n",
    "    control,\n",
    "    create_model,\n",
    "    execute,\n",
    "    prepare_state,\n",
    "    qfunc,\n",
    "    show,\n",
    "    synthesize,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1ead3e7b-b151-4f0f-9fec-e2c8c0918cb2",
   "metadata": {},
   "source": [
    "## 1/8 Building your algorithm\n",
    "**On the IDE:** Click on the `Model` tab to start writing your quantum model."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6f8c7857-b7fb-4d9f-86ef-afd6b801cd29",
   "metadata": {},
   "source": [
    "## 2/8 Build the model\n",
    "**On the IDE:** Here you define the model, function parameters, and more.\n",
    "See the User Guide [[2](#User_Guide)] for details.\n",
    "\n",
    "*Below is the SDK representation of the qmod syntax shown on the IDE page:*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d72f0b6a-6977-4cb3-ba4e-30930119f653",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T14:27:40.847549Z",
     "iopub.status.busy": "2024-05-07T14:27:40.846158Z",
     "iopub.status.idle": "2024-05-07T14:27:40.868727Z",
     "shell.execute_reply": "2024-05-07T14:27:40.868053Z"
    }
   },
   "outputs": [],
   "source": [
    "probabilities = [\n",
    "    0,\n",
    "    0.002,\n",
    "    0.004,\n",
    "    0.006,\n",
    "    0.0081,\n",
    "    0.0101,\n",
    "    0.0121,\n",
    "    0.0141,\n",
    "    0.0161,\n",
    "    0.0181,\n",
    "    0.0202,\n",
    "    0.0222,\n",
    "    0.0242,\n",
    "    0.0262,\n",
    "    0.0282,\n",
    "    0.0302,\n",
    "    0.0323,\n",
    "    0.0343,\n",
    "    0.0363,\n",
    "    0.0383,\n",
    "    0.0403,\n",
    "    0.0423,\n",
    "    0.0444,\n",
    "    0.0464,\n",
    "    0.0484,\n",
    "    0.0504,\n",
    "    0.0524,\n",
    "    0.0544,\n",
    "    0.0565,\n",
    "    0.0585,\n",
    "    0.0605,\n",
    "    0.0625,\n",
    "]\n",
    "\n",
    "\n",
    "@qfunc\n",
    "def main(io: Output[QArray[QBit]]):\n",
    "    prepare_state(probabilities=probabilities, bound=0.01, out=io)\n",
    "\n",
    "\n",
    "model = create_model(main)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e965d8d7-1f09-4e1a-9f4c-80a5263e749a",
   "metadata": {},
   "source": [
    "We can dump the model to file:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "0458676b-b479-48e8-b154-120a193a4f31",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T14:27:40.873903Z",
     "iopub.status.busy": "2024-05-07T14:27:40.872680Z",
     "iopub.status.idle": "2024-05-07T14:27:40.888315Z",
     "shell.execute_reply": "2024-05-07T14:27:40.887671Z"
    }
   },
   "outputs": [],
   "source": [
    "from classiq import write_qmod\n",
    "\n",
    "write_qmod(model, \"prepare_state\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bbcd1f17-91ce-48b9-a2d1-395b0affbf77",
   "metadata": {},
   "source": [
    "## 3/8 Synthesize the model\n",
    "**On the IDE:**  Now that you have selected or built a model, click the \"Synthesize\" button, sit back, and let Classiq do its magic!\n",
    "\n",
    "*Below is the SDK representation of the qmod syntax shown on the IDE page:*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "c0e3a1b4-de41-45df-a529-92f5f1be3dbc",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T14:27:40.896766Z",
     "iopub.status.busy": "2024-05-07T14:27:40.895783Z",
     "iopub.status.idle": "2024-05-07T14:27:45.821609Z",
     "shell.execute_reply": "2024-05-07T14:27:45.820910Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Opening: https://platform.classiq.io/circuit/ab291813-78a4-4491-9d45-ebbbff1388e2?version=0.41.0.dev39%2B79c8fd0855\n"
     ]
    }
   ],
   "source": [
    "qprog = synthesize(model)\n",
    "show(qprog)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "318265cf-1243-4212-bb7b-dcf7c179ddac",
   "metadata": {},
   "source": [
    "## 4/8 Congratulations!\n",
    "**On the IDE:** This is your first quantum program!\n",
    "Learn more in the User Guide [[2](#User_Guide)]."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aad110e3-1093-4ad1-8898-77e46e9b3d16",
   "metadata": {},
   "source": [
    "## 5/8 Run your synthesized quantum program on quantum hardware or simulators\n",
    "**On the IDE:** Press the 'Execute' button to define the quantum hardware or a quantum simulator to run your quantum program."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2d5f2c00-59c3-4ec2-b8d3-1275a357241d",
   "metadata": {},
   "source": [
    "## 6/8 Execution details\n",
    "**On the IDE:** Select which quantum program to execute, define execution parameters and choose a quantum provider and backend platform.\n",
    "The Classiq Platform is your gateway to all major quantum computing providers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d04e51f1-7d2c-481a-8b51-2bdd00a0ecff",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T14:27:45.824805Z",
     "iopub.status.busy": "2024-05-07T14:27:45.824367Z",
     "iopub.status.idle": "2024-05-07T14:27:45.889723Z",
     "shell.execute_reply": "2024-05-07T14:27:45.888920Z"
    }
   },
   "outputs": [],
   "source": [
    "from classiq.execution import (\n",
    "    ClassiqBackendPreferences,\n",
    "    ClassiqSimulatorBackendNames,\n",
    "    ExecutionPreferences,\n",
    "    set_quantum_program_execution_preferences,\n",
    ")\n",
    "\n",
    "preferences = ExecutionPreferences(\n",
    "    backend_preferences=ClassiqBackendPreferences(\n",
    "        backend_name=ClassiqSimulatorBackendNames.SIMULATOR\n",
    "    )\n",
    ")\n",
    "qprog = set_quantum_program_execution_preferences(qprog, preferences)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dcae72f4-22cf-4782-a5a3-33f61d7dbff8",
   "metadata": {},
   "source": [
    "## 7/8 Let's run your quantum program on a quantum simulator!\n",
    "**On the IDE:** Press 'Run' button to execute your quantum program on the simulator which we chose in the previous step\n",
    "\n",
    "*Bellow is the simple SDK execute code:*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "799db6c3-7b10-4cbc-8748-af17f6516671",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T14:27:45.893748Z",
     "iopub.status.busy": "2024-05-07T14:27:45.893476Z",
     "iopub.status.idle": "2024-05-07T14:27:48.602663Z",
     "shell.execute_reply": "2024-05-07T14:27:48.601841Z"
    }
   },
   "outputs": [],
   "source": [
    "job = execute(qprog)\n",
    "results = job.result()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "ed5e73cf-5cb8-436c-8794-b468a43095fd",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T14:27:48.607691Z",
     "iopub.status.busy": "2024-05-07T14:27:48.605960Z",
     "iopub.status.idle": "2024-05-07T14:27:48.611472Z",
     "shell.execute_reply": "2024-05-07T14:27:48.610706Z"
    }
   },
   "outputs": [],
   "source": [
    "job.open_in_ide()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "168fd57f-b83e-4518-a556-b92123c836fa",
   "metadata": {},
   "source": [
    "## 8/8 Wow! Look at that cool triangle probability function!\n",
    "**On the IDE:** That's it! You ran your first quantum program.\n",
    "\n",
    "\n",
    "To learn more about the Classiq Platform visit our User Guide [[2](#User_Guide)]."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7e20aa4c-19c7-4ca0-99d2-fcc5f830f59d",
   "metadata": {},
   "source": [
    "## References\n",
    "\n",
    "<a name='Classiq_IDE'>[1]</a>: [Classiq IDE](https://platform.classiq.io/).\n",
    "\n",
    "<a name='User_Guide'>[2]</a>: [Classiq User_Guide](https://docs.classiq.io/latest/).\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
